package codetop.microsoft.T33;

/**
 * @Author: 18362
 * @Create: 2022-08-13 22:45:15 星期六
 */
class Solution {
    public int search(int[] nums, int target) {
        int left = 0, right = nums.length-1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target)
                return mid;
            if (nums[left] == target || nums[right] == target)
                return nums[left] == target ? left : right;
            if (nums[left] < nums[right]) {
                if (target < nums[left] || target > nums[right])
                    return -1;
                else if (nums[mid] > target)
                    right = mid - 1;
                else
                    left = mid + 1;
            } else {
                if (target < nums[left] && target > nums[right])
                    return -1;
                else if (target > nums[left]) {
                    if (nums[mid] <= nums[right])
                        right = mid - 1;
                    else if (target > nums[mid])
                        left = mid + 1;
                    else
                        right = mid - 1;
                } else {
                    if (nums[mid] >= nums[left])
                        left = mid + 1;
                    else if (target < nums[mid])
                        right = mid - 1;
                    else
                        left = mid + 1;
                }
            }
        }
        return nums[left] == target ? left : -1;
    }
}